package com.sea.human.controller;


import com.codecrane.core.power.service.PowerGroupService;

import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.human.entity.Performance;
import com.sea.human.service.PerformanceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/human/performance/dashboard")
public class PerformanceDashboardController {

    @Autowired
    private PerformanceService performanceService;
    @Autowired
    private PowerGroupService powerGroupService;

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("performanceNumber",performanceService.findPerformanceNumber(null));
        return "/sea/human/performanceManage/dashBoard";
    }

    @ResponseBody
    @RequestMapping("/findStatusNumber")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Performance performance) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<Performance> performanceList = performanceService.findStatusNumber(performance);
        PageInfo<Performance> page = new PageInfo<>(performanceList);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /*
     * 获取未完成数量总和
     */
    @RequestMapping("/findPerformanceNumber")
    @ResponseBody
    public AjaxReturn findPerformanceNumber() throws ParseException {
        AjaxReturn result = Ajax.fail().setMsg("获取数据失败");
        result.setOk(true).setMsg("").setData(performanceService.findPerformanceNumber(null));
        return result;
    }

}
